/****************************************************************************************
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.service.util;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.math.NumberUtils;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttrDTO;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 *
 * @author tengg<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月29日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.util <br>
 */

public class OfferDealUtils {

    public static void setProdInstAttrValue(String sceneInstId, KiteInst prodInst, List<KitePosAttrDTO> prodInstAttrs) {
        if(KiteListUtils.isEmpty(prodInstAttrs)){
            return;
        }
        List<KiteAttr> attrs = KiteListUtils.isNotEmpty(prodInst.listAttrs()) ?
            prodInst.listAttrs() : new ArrayList<>();
        attrs.sort(Comparator.comparing(attr -> NumberUtils.toInt(attr.getOrderId(), 0)));
        OperateInstAttrService operateInstAttrService = SpringUtil.getBean(OperateInstAttrService.class);
        for (KiteAttr kiteAttr : attrs) {
            String fieldName = kiteAttr.getFieldName();

            List<KitePosAttrDTO> posAttrs = prodInstAttrs.stream().filter((attr) -> fieldName.equals(attr.getCode())).collect(Collectors.toList());
            //判断属性是否在极简端存在，存在则直接更新
            if (!KiteListUtils.isEmpty(posAttrs)) {
                KitePosAttrDTO attr = posAttrs.get(0);
                String value = attr.getValue();

                // 兼容存量数据时间格式不同情况（yyyy/MM/dd）
                if(KiteStringUtils.isNotEmpty(value) && KiteStringUtils.isEqual(kiteAttr.getAttrValueTypeId(), "98D")) {
                    value = value.replaceAll("/", "-");
                }

                if (!KiteStringUtils.isEmpty(value) && KiteStringUtils.isIndexOf(KeyConsts.LOAD_ATTR_VALUE_CTRL, kiteAttr.getAttrValueTypeId(), "/")) {
                    if (KiteListUtils.isEmpty(kiteAttr.getAttrValues())) {
                        List<AttrValue> attrValueList = AttrValueCache.get(kiteAttr.getAttrId());
                        if (KiteListUtils.isNotEmpty(attrValueList)) {
                            kiteAttr.setAttrValues(
                                KiteBeanUtils.copyPropsForList(attrValueList, KiteAttrValue.class));
                        }
                    }
                }
                if (!KiteStringUtils.equals(attr.getChangeType(), ChangeTypeUtils.CHANGE_TYPE_A)) {
                    kiteAttr.setOriginValue(Optional.ofNullable(attr.getOriginValue()).orElse(attr.getValue()));
                }
                kiteAttr.setOldValue(value);
                operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), value, sceneInstId, null);

                kiteAttr.setStartDate(attr.getStartDate());
                kiteAttr.setEndDate(attr.getEndDate());
                prodInst.setAttr(kiteAttr);
            }
        }
    }

    public static String dealFunProdInstActionType (List<KitePosAttrDTO> prodInstAttrs) {
        if (KiteListUtils.isEmpty(prodInstAttrs)) {
            return "";
        }
        prodInstAttrs.removeIf(attrs -> KiteStringUtils.equalsAny(attrs.getCode(), "masterSla","CUST_TYPE","changeType"));
        //新增属性个数
        int addCount = 0;
        // 删除属性个数
        int delCount = 0;
        // 不变属性个数
        int noChange = 0;
        // 属性中是否存在changeType属性值
        boolean flag = false;
        for (KitePosAttrDTO instAttr : prodInstAttrs) {
            if (KiteStringUtils.isEmpty(instAttr.getChangeType())) {
                continue;
            }
            flag = true;
            if (KiteStringUtils.isEqual(instAttr.getChangeType(), ChangeTypeUtils.CHANGE_TYPE_A)) {
                addCount = addCount + 1;
                continue;
            }
            if (KiteStringUtils.isEqual(instAttr.getChangeType(), ChangeTypeUtils.CHANGE_TYPE_D)) {
                delCount = delCount + 1;
                continue;
            }
            if (KiteStringUtils.isEqual(instAttr.getChangeType(), ChangeTypeUtils.CHANGE_TYPE_K)) {
                noChange = noChange + 1;
                continue;
            }
        }
        if (flag) {
            if (addCount == prodInstAttrs.size()
                || (delCount + addCount == prodInstAttrs.size() && delCount < addCount)) {
                return ChangeTypeUtils.CHANGE_TYPE_A;
            }
            if (delCount == prodInstAttrs.size()
                || (delCount + noChange == prodInstAttrs.size() && noChange < delCount)) {
                return ChangeTypeUtils.CHANGE_TYPE_D;
            }
            if (noChange == prodInstAttrs.size()
                || (delCount + noChange == prodInstAttrs.size() && noChange > delCount)
                || (addCount + noChange == prodInstAttrs.size() && noChange > addCount)) {
                return ChangeTypeUtils.CHANGE_TYPE_K;
            }
            return ChangeTypeUtils.CHANGE_TYPE_M;
        }
        else {
            // 没有changetype时返回空
            return "";
        }
    }
}
